package com.gitee.feizns.explore.data_structure.tree.bs.mid;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 515. 在每个树行中找最大值
 * @author feizns
 * @since 2020/3/10
 */
public class LargestValues {

    public static void main(String[] args) {

    }

    public static List<Integer> largestValues(TreeNode root) {
        List<Integer> ret = new LinkedList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if ( root != null )
            queue.add(root);
        while ( !queue.isEmpty() ) {
            int size = queue.size();
            TreeNode max = queue.peek();
            for (int i = 1; i < size; i++) {
                TreeNode node = queue.remove();
                if ( node.val > max.val )
                    max = node;
                if ( node.left != null )
                    queue.add(node.left);
                if ( node.right != null )
                    queue.add(node.right);
            }
            ret.add(max.val);
        }
        return ret;
    }

}
